package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.entity.StudentRecord;
import org.example.xsda.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 档案记录控制器
 */
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @GetMapping("/page")
    public Result<PageResult<StudentRecord>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Integer recordType
    ) {
        PageResult<StudentRecord> pageResult = recordService.page(current, size, studentId, recordType);
        return Result.success(pageResult);
    }

    @GetMapping("/student/{studentId}")
    public Result<List<StudentRecord>> getByStudentId(@PathVariable Long studentId) {
        List<StudentRecord> list = recordService.getByStudentId(studentId);
        return Result.success(list);
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody StudentRecord record) {
        recordService.add(record);
        return Result.success();
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @PutMapping
    public Result<Void> update(@Validated @RequestBody StudentRecord record) {
        recordService.update(record);
        return Result.success();
    }

    @SaCheckRole("admin")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        recordService.delete(id);
        return Result.success();
    }
}

